package org.iku.common;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTool {

	
	public static String singleExtract(String content,String regex,int group){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		if(matcher.find()){
			return matcher.group(group);
		}
		return null;
	}
	
	public static List<MatchResult> multiExtractToMatchResult(String content,String regex){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		List<MatchResult> matchResults = new ArrayList<MatchResult>();
		while(matcher.find()){
			matchResults.add(matcher.toMatchResult());
		}
		return matchResults;
	}
	
	public static List<String> multiExtract(String content,String regex){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		List<String> matchResults = null;
		if(matcher.matches()){
			matchResults = new ArrayList<String>();
			for(int i = 0;i<matcher.groupCount();i++){
				matchResults.add(matcher.group(i));
			}
		}
		return matchResults;
	}
	
	public static List<String> singleExtractToList(String content,String regex,int group){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		List<String> strings = new ArrayList<String>();
		while(matcher.find()){
			strings.add(matcher.group(group));
		}
		return strings;
	}
	
	public static MatchResult singleExtractToMatchResult(String content,String regex){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()){
			return matcher.toMatchResult();
		}
		return null;
	}
	
	public static void main(String[] args) {
		String a = "asd------asd";
		List<String> r = RegexTool.multiExtract(a, "(asd).*?(asd)");
		
		System.out.println(r.get(0));
	}
	
}
